from orion.util.FileUtil import FileUtil
import os
import tempfile
import time
import unittest

class FileUtilTest(unittest.TestCase):
    
    def setUp(self):
        dir = "%s_%s" % ("fileUtilTest_", time.time())
        osTempDir = tempfile.gettempdir()
        self.testTempDir = "%s\\%s" % (osTempDir, dir)
        os.mkdir(self.testTempDir)

    def tearDown(self):
        for file in os.listdir(self.testTempDir):
            os.remove(os.path.join(self.testTempDir, file))
        os.rmdir(self.testTempDir)

    def testWrite(self):
        mockFile = os.path.join(self.testTempDir, "mock.srt")
        mockContent = "MyMock"
        FileUtil.write(mockFile, mockContent)
        
        try:
            writtenFile = open(mockFile, "r")
            writtentContent = writtenFile.read()
            self.assertEqual(mockContent, writtentContent)
        except:
            self.fail("Failed to open written file")
        finally:
            writtenFile.close()
            
    def testInfo(self):
        mockPath = ["E:\\mockDir", "\\", "mockFile", ".", "txt"]
        path, title = FileUtil.info("".join(mockPath))

        self.assertEqual(path, mockPath[0])
        self.assertEqual(title, mockPath[2])

if __name__ == "__main__":
    unittest.main()